package cn.kgc.controller;

import cn.kgc.pojo.*;
import cn.kgc.service.*;
import cn.kgc.util.Page;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Component
@EnableScheduling
public class backStageController {
    @Reference
    private CompanyService companyService;
    @Reference
    private JobsubscriptionService jobsubscriptionService;
    @Reference
    private QiuzhipositionService qiuzhipositionService;
    @Reference
    private JobpostingService jobpostingService;
    @Reference
    private PositionpushService positionpushService;
    @Reference
    private PositionService positionService;
    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 查看所有求职者订阅职位信息
     */

    @RequestMapping("/getSubscription.json")
    public Page jobsubscriptionfindAll(String subscriptionTime,Integer index, Integer size){
        Page page=jobsubscriptionService.queryByPage(subscriptionTime,index,size);
        return page;

    }
    /**
     * 推送岗位
     */

    @Scheduled(cron = "0 0 12 * * ?")
    public void push(){
        //查找所有未推送的订阅信息
        List<Jobsubscription> subscription= jobsubscriptionService.findAllByPushState();
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //循环所有未推送的订阅信息
        for (int sub=0;sub<subscription.size();sub++){
            //查找符合条件的职位信息
        String job=subscription.get(sub).getJobtitle();
        Integer positionId =positionService.findByName(job);
        String  monthly=subscription.get(sub).getSalaryRange();
        String  workCity=subscription.get(sub).getWorkcity();
        List<Jobposting> jobposting= jobpostingService.selectJobposting(positionId,workCity,monthly);
            //根据应聘者Id查找推送记录
        Integer applicant=subscription.get(sub).getId();
        List<Positionpush> positionpushes=positionpushService.findByUserName(applicant);
            //用来记录发送了几条公司信息
            int cishu=0;
            String str=" ";
            //循环所有符合条件的岗位
        for (int i=0; i<jobposting.size();i++){
            //如果该求职者已经推送过并且推送的次数小于5次
            if (positionpushes!=null){
                //当推送的次数小于5次
                if (cishu<5){
                    for (int j=0;j<jobposting.size();j++){
                        //判断当前这个岗位是否已经推送过，如果没有推送过则推送 推送的次数+1
                        if (positionpushes.get(j).getJobpostingid()!=jobposting.get(i).getResumeid()){
                            str=str+"  公司名称："+jobposting.get(i).getCompanyName()+"职位名称:"+jobposting.get(i).getPosition()+
                                    " 月薪范围："+jobposting.get(i).getMonthly()+"所在城市："+jobposting.get(i).getWorkCity();
                            positionpushService.save(jobposting.get(i).getResumeid(),applicant);
                            cishu++;
                        };

                    };
                };
             }else{
                if (cishu<5){
                    str=str+"  公司名称："+jobposting.get(i).getCompanyName()+"职位名称:"+jobposting.get(i).getPosition()+
                            " 月薪范围："+jobposting.get(i).getMonthly()+"所在城市："+jobposting.get(i).getWorkCity();
                    positionpushService.save(jobposting.get(i).getResumeid(),applicant);
                    cishu++;
                };
            }
        };
        //将5条信息发送给求职者
            mailMessage.setFrom("liuchuang_java@126.com");
            mailMessage.setTo(subscription.get(sub).getEmail());
            mailMessage.setSubject("岗位推荐");
            mailMessage.setText(str);
            javaMailSender.send(mailMessage);

      }

    }
}
